package com.jh.paymentchannel.util.xjy;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Map.Entry;

@SuppressWarnings("serial")
public abstract class QuickPayServlet extends HttpServlet {

    protected Logger LOG = LoggerFactory.getLogger(getClass());

    protected void setCharsetEncoding(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
    }

    public void doOutPrint(HttpServletResponse response, QuickPayRespModel jsonMessage) throws IOException {
        String html = jsonMessage.getMsg();
        if (StringUtils.isBlank(html)) {
            Map<String, Object> respData = jsonMessage.getAttributes();
            if (respData != null && respData.size() > 0) {
                for (Entry<String, Object> entry : respData.entrySet()) {
                    html += entry.getKey() + "|" + entry.getValue() + "<br>";
                }
            }
        }
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print(html);
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

    public void doOutPrint4Attr(HttpServletResponse response, QuickPayRespModel jsonMessage) throws IOException {
        Map<String, Object> respData = jsonMessage.getAttributes();
        StringBuilder html = new StringBuilder();
        if (respData != null && respData.size() > 0) {
            for (Entry<String, Object> entry : respData.entrySet()) {
                html.append(entry.getKey()).append("|").append(entry.getValue()).append("<br>");
            }
        } else {
            html.append(jsonMessage.getErrorCode() + "|" + jsonMessage.getMsg());
        }
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>中国银联</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print(html.toString());
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

    public void doOutPrint(HttpServletResponse response, Exception e) throws IOException {
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print(e.getMessage());
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

    public void doOutPrint(HttpServletResponse response, String html) throws IOException {
        PrintWriter out = response.getWriter();
        out.print(html);
        out.flush();
        out.close();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        setCharsetEncoding(request, response);
        doExtGet(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        setCharsetEncoding(request, response);
        doExtPost(request, response);
    }

    public abstract void doExtGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;

    public abstract void doExtPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}
